<template>
	<view class="content">
		<view v-if="isLogin">
			<image class="headicon" @click="changeHeadIcon" :src="'http://localhost:8090/foodapp/'+userinfo.icon">
			</image>
			<view class="text-area" v-if="isLogin">
				<text class="title">{{userinfo.username}}</text>
			</view>
		</view>
		<view v-if="!isLogin">
			<image class="headicon" :src="userinfo.icon"></image>
			<view class="text-area">
				<button type="primary" size="mini" @click="toLogin">点击登录</button>
			</view>
		</view>
		<view class="entry-wrapper">
			<view class="item">
				<text>个人信息</text>
				<image src="../../static/右括号.png" mode=""></image>
			</view>
			<view class="item">
				<text>订单列表</text>
				<image src="../../static/右括号.png" mode=""></image>
			</view>
			<view class="item" @click="toInfoPage">
				<text>个人信息</text>
				<image src="../../static/右括号.png" mode=""></image>
			</view>
			<view class="item">
				<text>购物车</text>
				<image src="../../static/右括号.png" mode=""></image>
			</view>
		</view>
		<button type="primary" @click="logout">退出</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userinfo: {
					userid: null,
					icon: '',
					nickname: '',
					username: null
				},
				isLogin: false
			}

		},
		methods: {
			getUserInfo() {
				let userStr = uni.getStorageSync('userInfo')
				console.log(userStr);
				if (userStr) {
					this.userinfo = userStr
					this.isLogin = true
					console.log('用户已登录，读取成功');
				} else {
					this.userinfo.username = null
					this.userinfo.nickname = null
					this.userinfo.icon = '/static/my.png'
					this.isLogin = false
				}
			},
			toInfoPage() {
				uni.navigateTo({
					url: './xinxi?item=' + encodeURIComponent(JSON.stringify(this.userinfo))
				});
			},
			logout() {
				uni.setStorageSync('userInfo', null)
				this.getUserInfo()
				uni.navigateTo({
					url: '/pages/my/login'
				})
			},
			changeHeadIcon() {
				uni.chooseImage({
					count: 1,
					success: (choosRes) => {
						let filePath = choosRes.tempFilePaths[0]
						console.log(choosRes)
						uni.uploadFile({
							url: this.$baseUrl + 'user/upload_headpic',
							filePath: filePath,
							name: 'file',
							formData: {
								id: this.userinfo.id
							},
							success: (uploadRes) => {
								// 更新本地存储，更新页面
								console.log(uploadRes)
								let resData = JSON.parse(uploadRes.data)
								console.log(resData)
								// uni.setStorageSync('userInfo', result.data)
								this.userinfo.icon = resData.data.icon
							}
						})
					}
				})
			},
			logout() {
				uni.setStorageSync('userInfo', null)
				this.getUserInfo()
				uni.navigateTo({
					url: '/pages/my/login'
				})

			},
			toLogin() {
				uni.navigateTo({
					url: '/pages/my/login'
				})
			},

		},
		onLoad() {

		},
		onShow() {
			this.getUserInfo();
		},
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		width: 100%;
		height: 100%;


	}

	.headicon {
		width: 150rpx;
		height: 150rpx;
		border-radius: 50%;
		margin: 100rpx;
		border: 1px deepskyblue solid;
	}

	.entry-wrapper {
		width: 90%;
		/* background-color: #888; */
		margin-top: 50rpx;
	}

	.entry-wrapper .item {

		display: flex;
		flex-direction: row;

		justify-content: space-between;

		height: 80rpx;

		align-items: center;

		border-bottom: #333 solid 1rpx;
	}

	.entry-wrapper .item:first-child {

		border-top: #333 solid 1rpx;
	}

	.entry-wrapper .item image {
		width: 48rpx;
		height: 48rpx;
	}

	button {
		width: 95%;
		margin-top: 50rpx;
	}
</style>